<?php
class MozgParams extends MozgProperty{
	
	protected $_params = array();
	static protected $_allParamsConfig = array();
	protected $_isSetConfig = false;
	protected $_configParamsPath;
	
	/**
	 * 
	 * __construct($paramsPath [, $params, $params ...])
	 */
	public function __construct($paramsPath)
	{
		$this->SetParamsConfig($paramsPath);
		
		$args = func_get_args();
		array_shift($args);
		call_user_func_array(array($this, 'AddData'), $args);
	}
	
	protected function SetParamsConfig($paramsPath)
	{
		if (!$paramsPath) return $this;
		
		$this->_configParamsPath = $paramsPath;
		
	    if (!self::$_allParamsConfig[$paramsPath])
    	{
    		$paramsPath = trim($paramsPath, '/');
    		$paramsFilePath = _APP_PATH. 'config/' .$paramsPath. '.php';
    		if (!is_file($paramsFilePath)) throw new Exception('Не найден файл параметров ' .$paramsFilePath);
    		
    		$fields = include($paramsFilePath);
    		$fields['fields'] = $this->lData((array)$fields['fields']);
    	
    		$newFields = array(
    			'title' => '', 'values' => array(), 'params' => array(),
    		);
    		foreach ($fields['fields'] as $fieldName => $fieldData)
    		{
    			$newFields['values'][$fieldName] = $fieldData['value'];
    			if (isset($fieldData['params']) && is_array($fieldData['params']))
    			{
    				$newFields['params'][$fieldName] = $fieldData['params'];
    			}	
    		}
    		
    		$newFields['title'] = $fields['title'];
    		self::$_allParamsConfig[$paramsPath] = $newFields;
    	}		
		
    	$this->_isSetConfig = true;
		
		return $this;
	}
	
	public function IsSetConfig()
	{
		return $this->_isSetConfig;
	}
	
	public function GetConfigPath()
	{
		return $this->_configParamsPath;
	}
	
	public function GetConfigTitle()
	{
		if (!$this->IsSetConfig()) return '';
		return self::$_allParamsConfig[$this->GetConfigPath()]['title']; 
	}

	public function GetConfigFields()
	{
		if (!$this->IsSetConfig()) return array();
		return self::$_allParamsConfig[$this->GetConfigPath()]['params']; 
	}	
	
	public function GetConfigField($fieldName)
	{
		if (!$this->IsSetConfig()) return false;
		
		$fieldName = $this->lName($fieldName);
		return isset(self::$_allParamsConfig[$this->GetConfigPath()]['params'][$fieldName]) ? 
				self::$_allParamsConfig[$this->GetConfigPath()]['params'][$fieldName] : false; 
	}

    public function Get($fieldName)
    {
        $value = parent::Get($fieldName);
        if (is_null($value)) $value = $this->GetDefault($fieldName);

        return $value;
    } 	
	
	public function GetDefault($fieldName)
	{
    	$nameParts = explode('/', $fieldName, 2);
    	$fieldName = $this->lName(array_shift($nameParts));
    			
		return $this->GetPath(isset($nameParts[0]) ? $nameParts[0] : '', 
								isset(self::$_allParamsConfig[$this->GetConfigPath()]['values'][$fieldName]) ? 
									self::$_allParamsConfig[$this->GetConfigPath()]['values'][$fieldName] : null);
	}
	
	public function SetData($rawParams)
	{
		$this->ClearData();
		return $this->AddData($rawParams);		
	}

	public function AddData()
	{
		$args = func_get_args();

		foreach ($args as &$rawParams)
		{		
			if (is_array($rawParams))
			{
				continue;
			}	
			else if ($rawParams instanceof self)
			{
				$rawParams = $rawParams->GetData();
			}
			else
			{
				$rawParams = $this->UnpackData($rawParams);
			}		
		}
		
		call_user_func_array(array(parent, 'AddData'), $args);

		return $this;		
	}
	
	protected function UnpackData($rawParams)
	{
		$params = array();
		if (!$rawParams) return $params;
		
		$rows = explode("\n", trim($rawParams));
		foreach ($rows as $row)
		{
			list($key, $packedValue) = explode('=', $row, 2);
			$params[$key] = unserialize(urldecode($packedValue));
		}
		
		return $params;
	}

	public function GetOriginalData()
	{
		$fields = array_keys($this->GetConfigFields());
		$origParams = array();
		foreach ($fields as $field)
		{
			if (!$this->HasSet($field)) continue;
			$origParams[$field] = $this->Get($field);	
		}
		
		return $origParams;
	}	
	
	public function GetRawData($params)
	{
		$rawParams = array();
		
		foreach ($params as $key => $value)
		{
			$rawParams[] = $key .'=' .urlencode(serialize($value));
		}
		
		return implode("\n", $rawParams);
	}
	
	public function __toString()
	{
		return $this->GetRawData($this->GetOriginalData());
	}	
}